package org.kfive.nova.crawler;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.pipeline.ConsolePipeline;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Selectable;

import java.util.List;

/*
* XDYPageProcessor.java
* Created  by  lizhanwu
* Created on 2016-7-29 10:45
*/
public class XDYPageProcessor implements PageProcessor {

    //抓取域名
    private Site site;

    public XDYPageProcessor(Site site){
        this.site = site;
    }

    public void process(Page page) {
        //获取当前页面待抓取URL
        List<String> links = page.getHtml().links().regex("forum-\\d*-\\d*\\.html").all();
        for (String url : links){
            page.addTargetRequest("http://www.xiaodaiya.com/"+url);
        }
        Selectable selectable = page.getHtml().xpath("//tbody/tr/th/a[@class='s xst']/text()");
        List<String> selectValue = selectable.all();
        page.putField("title",selectValue);
        System.out.print("");
    }

    public Site getSite() {
        return this.site;
    }

    public static void main(String[] args) {
        Site site = Site.me().setRetryTimes(1).setSleepTime(5000);
        Spider.create(new XDYPageProcessor(site)).addUrl("http://www.xiaodaiya.com/forum-2-1.html").addPipeline(new ConsolePipeline()).thread(1).run();
    }
}
